//
//  Device+Utils.swift
//  casdon_swiftUI
//
//  Created by casdon on 2025/4/24.
//

import Foundation
import UIKit

extension UIDevice {
    static func currentDevice() -> String
    {
        return getIPhoneModel()
    }
}

fileprivate func deviceName(from identifier: String) -> String {
    let deviceNames = [
        "iPhone6,1" : "iPhone 5s",
        "iPhone6,2" : "iPhone 5s (GSM+CDMA)",
        "iPhone7,1" : "iPhone 6 Plus",
        "iPhone7,2" : "iPhone 6",
        "iPhone8,1" : "iPhone 6s",
        "iPhone8,2" : "iPhone 6s Plus",
        "iPhone8,4" : "iPhone SE (1st generation)",
        "iPhone9,1" : "iPhone 7",
        "iPhone9,2" : "iPhone 7 Plus",
        "iPhone9,3" : "iPhone 7",
        "iPhone9,4" : "iPhone 7 Plus",
        "iPhone10,1" : "iPhone 8",
        "iPhone10,2" : "iPhone 8 Plus",
        "iPhone10,3" : "iPhone X",
        "iPhone10,4" : "iPhone 8",
        "iPhone10,5" : "iPhone 8 Plus",
        "iPhone10,6" : "iPhone X",
        "iPhone11,2" : "iPhone XS",
        "iPhone11,4" : "iPhone XS Max",
        "iPhone11,6" : "iPhone XS Max (China)",
        "iPhone11,8" : "iPhone XR",
        "iPhone12,1" : "iPhone 11",
        "iPhone12,3" : "iPhone 11 Pro",
        "iPhone12,5" : "iPhone 11 Pro Max",
        "iPhone12,8" : "iPhone SE (2nd generation)",
        "iPhone13,1" : "iPhone 12 Mini",
        "iPhone13,2" : "iPhone 12",
        "iPhone13,3" : "iPhone 12 Pro",
        "iPhone13,4" : "iPhone 12 Pro Max",
        "iPhone14,4" : "iPhone 13 Mini",
        "iPhone14,5" : "iPhone 13",
        "iPhone14,2" : "iPhone 13 Pro",
        "iPhone14,3" : "iPhone 13 Pro Max",
        "iPhone14,6" : "iPhoneSE3",
        
        "iPhone14,7" : "iPhone14",
        "iPhone14,8" : "iPhone14Plus",
        "iPhone15.2" : "iPhone14Pro",
        "iPhone15.3" : "iPhone14ProMax",
        
        "iPhone15,4" : "iPhone15",
        "iPhone15,5" : "iPhone15Plus",
        "iPhone16,1" : "iPhone15Pro",
        "iPhone16,2" : "iPhone15ProMax",
        
        "iPhone17,1" : "iPhone16Pro",
        "iPhone17,2" : "iPhone16ProMax",
        "iPhone17,3" : "iPhone16",
        "iPhone17,4" : "iPhone16 Plus",
    ]
    
    return deviceNames[identifier] ?? identifier
}

fileprivate func getIPhoneModel() -> String {
    
    var systemInfo = utsname()
    
    uname(&systemInfo)
    
    let machineMirror = Mirror(reflecting: systemInfo.machine)
    
    let identifier = machineMirror.children.reduce("") { identifier, element in
        
        guard let value = element.value as? Int8, value != 0 else { return identifier }
        
        return identifier + String(UnicodeScalar(UInt8(value)))
    }
    
    return deviceName(from: identifier)
    
}

 
func getAppVersion() -> String {
    return Bundle.main.object(forInfoDictionaryKey: "CFBundleShortVersionString") as? String ?? ""
}
 
func getBuildVersion() -> String {
    return Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? ""
}
    
